
/* Kanvas Paint and Drawing Tool.
 * Copyright (C) 2011 Darryl Wallace
 * wallacdj@gmail.com / darryl@wallynet.ca
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*!
  @file EllipseTool.cpp
  @author Darryl Wallace
  @date 2011
  @brief This file defines the EllipseTool tool.
  */

#include "EllipseTool.h"


EllipseTool::EllipseTool()
    : _parent(NULL)
{
    _p.setWidthF(3.0);
}

EllipseTool::~EllipseTool()
{

}

void EllipseTool::setLayerItem(LayerItem *layer)
{
    setParentItem((QGraphicsItem *)layer );
    _parent=layer;
}

void EllipseTool::setPrimaryColor(const QColor &p)
{
    _primary=p;
    _p.setColor(_primary);
}

void EllipseTool::setSecondaryColor(const QColor &s)
{
    _secondary=s;
}

void EllipseTool::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setPen(_p);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->drawEllipse(boundingRect());
}

void EllipseTool::paintOnImage(QPainter *painter)
{
    painter->setPen(_p);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->drawEllipse(boundingRect());
}

void EllipseTool::handleMousePressEvent(const QGraphicsSceneMouseEvent *e)
{
    // Reset the painter path.
    _path=QPainterPath();

    _initial=e->scenePos();
    _last=e->scenePos();
    _current=e->scenePos();
}

void EllipseTool::handleMouseMoveEvent(const QGraphicsSceneMouseEvent *e)
{
    _last=_current;
    _current=e->scenePos();
}

void EllipseTool::handleMouseReleaseEvent(const QGraphicsSceneMouseEvent *e)
{
    // Set the last position.  Not currently used.
    _last=e->scenePos();
}

QRectF EllipseTool::boundingRect() const
{
    qreal x0=_initial.x();
    qreal y0=_initial.y();
    qreal x1=_current.x();
    qreal y1=_current.y();

    qreal x= x0;
    qreal width=x1-x0;
    qreal y =y0;
    qreal height = y1-y0;
    if (x1<x0)
    {
        x=x1;
        width=x0-x1;
    }
    if (y1<y0)
    {
        y=y1;
        height=y0-y1;
    }
    qreal penWidth=_p.widthF();

    return QRectF(x-penWidth/2.0, y-penWidth/2.0, width+penWidth, height+penWidth);
}

QIcon EllipseTool::toolIcon() const
{
    QIcon icon(":/KanvasStandardPlugin/icons/ellipse.png");
    return icon;
}

void EllipseTool::setPen(const QPen &pen)
{
    _p=pen;
}

